/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
								
	CREATES:	${out_tables}/T8_selection_unobs.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/
	
	
	* Load Data 
	use "${dt_final}/panel_RR_indexes.dta", clear


	gen femalechild=(gender==2)
	tab age_year, g(age)


/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
			
	* Controls
	*-----------------------------------------------------------
	macro  def Xvar1 "i.age_year i.gender" 
 	macro  def Xvar1old "i.age_year_old i.gender_old"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
    macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo" 
	
	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------
	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"


	xtset CENTRO_SALUD

	gen TIPOB=TIPO if TIPO!=3
	gen TIPOI=TIPO if TIPO!=2		
	gen single_old=(hh_tipo_old<=2)
	gen gender_old=sexo_old
	tab TIPO_old, g(group_old)

/*******************************************************************************
	Table 8: Selection on Unobservables 
*******************************************************************************/


	local finaloutcomes "zptevir_irtscore2 zsoc_index " 
	foreach var of local finaloutcomes {
	xi: xtreg `var' group2 group3 $Xvar1 if treat2==1, fe i(CENTRO_SALUD)
	estimates store `var'_b
	test group2 group3
	estadd scalar p1=r(p)

					
	xi: xtreg `var' group2 group3 $Xvar1 if treat3==1, fe i(CENTRO_SALUD)
	estimates store `var'_i
	test group2 group3
	estadd scalar p1=r(p)


	xi: xtreg `var' group2 group3 $Xvar1 if treat2==0 & treat3==0, fe i(CENTRO_SALUD)
	estimates store `var'_u
	test group2 group3
	estadd scalar p1=r(p)


	
	}			


	esttab  zptevir_irtscore2_b zptevir_irtscore2_i zptevir_irtscore2_u using ///
	"${out_tables}/T8_selection_unobs.csv", replace f 					  ///	   
	 keep(group2 group3 ) 				  ///
	 cells("b(fmt(3)star)" "se(fmt(3)par)") 								  ///
	 star(* 0.10 ** 0.05 *** 0.01) legend 								  ///
	 stats(p1 N, fmt(3 0) 									 		 ///
	 labels("p-value joint test NEP-B=NEP-I=0" "Observations")) ///
	 mtitles(	"Participants NEP-B"	"Participants NEP-I" "Non-Participants") /// 
	 varlabels(group2 "NEP-B" group3 "NEP-I") 					  ///
     title(Panel A: Vocabulary)


	esttab  zsoc_index_b zsoc_index_i zsoc_index_u  						  ///
	 using "${out_tables}/T8_selection_unobs.csv", append f 				  ///
	 keep(group2 group3 ) 				  ///
	 cells("b(fmt(3)star)" "se(fmt(3)par)") 								  ///
	 star(* 0.10 ** 0.05 *** 0.01) legend	  								  ///
	 stats(p1 N, fmt(3 0) 											  ///
	 labels("p-value joint test NEP-B=NEP-I=0" "Observations")) /// 
	 mtitles(	"Participants NEP-B"	"Participants NEP-I" "Non-Participants") /// 
	 varlabels(group2 "NEP-B" group3 "NEP-I") 					  			///
	 title(Panel B: Socioemotional Index)						

	
	
